package tankGame04;

import tankGame04.tank.Boom;
import tankGame04.tank.Bullet;
import tankGame04.tank.Enemy;
import tankGame04.tank.Hero;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.util.Vector;

import static tankGame04.tank.Bullet.BULLET_LENGTH;
import static tankGame04.tank.Bullet.BULLET_WIDTH;
import static tankGame04.tank.Tank.TANK_SIZE;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-01
 * Time:11:58
 */
@SuppressWarnings("all")
public class MyPanel extends JPanel implements KeyListener, Runnable {
    Hero hero = null;
    Vector<Enemy> enemies = new Vector<>();
    int enemySize = 3;
    Vector<Boom> booms = new Vector<>();
    Image boomImage1 = null;
    Image boomImage2 = null;
    Image boomImage3 = null;

    public MyPanel() {
        hero = new Hero(468, 660);
        for (int i = 0; i < enemySize; i++) {
            Enemy enemy = new Enemy(100 * (i + 1), 0, 2);
            enemies.add(enemy);
            enemy.shot();
            new Thread(enemy).start();
        }
        boomImage1 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/boom/boom1.png"));
        boomImage2 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/boom/boom2.png"));
        boomImage3 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/boom/boom3.png"));
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //draw background
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1000, 750);
        //draw hero
        if (hero.isState()) drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), TANK_SIZE, 0);
        //draw hero bullet
        Bullet heroBullet = hero.getBullet();
        if (heroBullet != null && heroBullet.isState()) {
            drawBullet(heroBullet.getX(), heroBullet.getY(), g, heroBullet.getDirect(), BULLET_WIDTH, BULLET_LENGTH, 0);
        }
        //draw enemies all elements
        for (Enemy enemy : enemies) {
            Vector<Bullet> enemyMagazine = enemy.getMagazine();
            if (enemyMagazine.size() > 0) {
                for (int i = 0; i < enemyMagazine.size(); i++) {
                    Bullet bullet = enemyMagazine.get(i);
                    if (bullet.isState()) {
                        drawBullet(bullet.getX(), bullet.getY(), g, bullet.getDirect(), BULLET_WIDTH, BULLET_LENGTH, 1);
                    } else {
                        enemyMagazine.remove(bullet);
                    }
                }
            }
            if (enemy.isState()) {
                drawTank(enemy.getX(), enemy.getY(), g, enemy.getDirect(), TANK_SIZE, 1);
            }
        }
        //draw boom
        for (int i = 0; i < booms.size(); i++) {
            Boom boom = booms.get(i);
            if (boom.isLife()) {
                if (boom.getLife() > 6) {
                    g.drawImage(boomImage1, boom.getX(), boom.getY(), TANK_SIZE, TANK_SIZE, this);
                } else if (boom.getLife() > 3) {
                    g.drawImage(boomImage2, boom.getX(), boom.getY(), TANK_SIZE, TANK_SIZE, this);
                } else {
                    g.drawImage(boomImage3, boom.getX(), boom.getY(), TANK_SIZE, TANK_SIZE, this);
                }
            } else {
                booms.remove(boom);
            }
        }
    }

    private void drawBullet(int x, int y, Graphics g, int diret, int width, int length, int role) {//role  0-hero's  1-enemy's
        if (role == 0) {
            drawHeroBullet(x, y, g, diret, width, length);
        } else {
            drawEnemyBullet(x, y, g, diret, width, length);
        }
    }

    private void drawHeroBullet(int x, int y, Graphics g, int direct, int width, int length) {
        Image image = switch (direct) {
            case 0 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/HeroBullet/heroBulletUp.png"));
            case 1 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/HeroBullet/heroBulletRight.png"));
            case 2 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/HeroBullet/heroBulletDown.png"));
            case 3 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/HeroBullet/heroBulletLeft.png"));
            default -> null;
        };
        switch (direct) {
            case 1:
            case 3:
                g.drawImage(image, x, y, length, width, this);
                break;
            case 0:
            case 2:
                g.drawImage(image, x, y, width, length, this);
                break;
        }
    }

    private void drawEnemyBullet(int x, int y, Graphics g, int direct, int width, int length) {
        Image image = switch (direct) {
            case 0 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/EnemyBullet/enemyBulletUp.png"));
            case 1 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/EnemyBullet/enemyBulletRight.png"));
            case 2 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/EnemyBullet/enemyBulletDown.png"));
            case 3 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/EnemyBullet/enemyBulletLeft.png"));
            default -> null;
        };
        switch (direct) {
            case 1:
            case 3:
                g.drawImage(image, x, y, length, width, this);
                break;
            case 0:
            case 2:
                g.drawImage(image, x, y, width, length, this);
                break;
        }
    }

    private void drawTank(int x, int y, Graphics g, int direct, int size, int role) {//role 0-hero 1-enemy
        if (role == 0) {
            drawHero(x, y, g, direct, size);
        } else {
            drawEnemy(x, y, g, direct, size);
        }
    }

    private void drawHero(int x, int y, Graphics g, int direct, int size) {
        Image image = switch (direct) {
            case 0 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/hero2/heroUp.png"));
            case 1 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/hero2/heroRight.png"));
            case 2 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/hero2/heroDown.png"));
            case 3 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/hero2/heroLeft.png"));
            default -> null;
        };
        g.drawImage(image, x, y, size, size, this);
    }

    private void drawEnemy(int x, int y, Graphics g, int direct, int size) {
        Image image = switch (direct) {
            case 0 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/enemy2/enemyUp.png"));
            case 1 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/enemy2/enemyRight.png"));
            case 2 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/enemy2/enemyDown.png"));
            case 3 ->
                    Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame04/enemy2/enemyLeft.png"));
            default -> null;
        };
        g.drawImage(image, x, y, size, size, this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
            hero.move(0, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
            hero.move(2, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
            hero.move(1, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
            hero.move(3, hero.getSpeed());
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shot();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            attactEnemy();
            attactHero();
            for (int i = 0; i < booms.size(); i++) {
                Boom boom = booms.get(i);
                if (boom.isLife()) {
                    boom.lifeDown();
                }
            }
            repaint();
        }
    }

    private void attactEnemy() {
        Bullet heroBullet = hero.getBullet();
        if (heroBullet != null && heroBullet.isState()) {
            int direct = heroBullet.getDirect();
            for (Enemy enemy : enemies) {
                if (enemy.isState()) {
                    int hbx, hby, ex, ey;
                    hbx = heroBullet.getX();
                    hby = heroBullet.getY();
                    ex = enemy.getX();
                    ey = enemy.getY();
                    switch (direct) {
                        case 0:
                            if (hbx >= ex && hbx <= ex + 50 && hby <= ey + 52 && hby >= ey) {
                                enemy.setState(false);
                                heroBullet.setState(false);
                                booms.add(new Boom(ex, ey));
                            }
                            break;
                        case 1:
                            if (hby >= ey && hby <= ey + 50 && hbx + 5 >= ex && hbx <= ex + 47) {
                                enemy.setState(false);
                                heroBullet.setState(false);
                                booms.add(new Boom(ex, ey));
                            }
                            break;
                        case 2:
                            if (hbx >= ex && hbx <= ex + 50 && hby + 5 >= ey && hby <= ey + 47) {
                                enemy.setState(false);
                                heroBullet.setState(false);
                                booms.add(new Boom(ex, ey));
                            }
                            break;
                        case 3:
                            if (hby >= ey && hby <= ey + 50 && hbx <= ex + 52 && hbx + 5 >= ex) {
                                enemy.setState(false);
                                heroBullet.setState(false);
                                booms.add(new Boom(ex, ey));
                            }
                            break;
                    }
                }
            }
        }
    }

    private void attactHero() {
        for (Enemy enemy : enemies) {
            Vector<Bullet> magazine = enemy.getMagazine();
            if (magazine.size() > 0) {
                for (int i = 0; i < magazine.size(); i++) {
                    Bullet enemyBullet = magazine.get(i);
                    if (enemyBullet != null && enemyBullet.isState()) {
                        int direct = enemyBullet.getDirect();
                        if (hero.isState()) {
                            int ebx, eby, hx, hy;
                            ebx = enemyBullet.getX();
                            eby = enemyBullet.getY();
                            hx = hero.getX();
                            hy = hero.getY();
                            switch (direct) {
                                case 0:
                                    if (ebx >= hx && ebx <= hx + 50 && eby <= hy + 52 && eby >= hy) {
                                        hero.setState(false);
                                        enemyBullet.setState(false);
                                        booms.add(new Boom(hx, hy));
                                    }
                                    break;
                                case 1:
                                    if (eby >= hy && eby <= hy + 50 && ebx + 5 >= hx && ebx <= hx + 47) {
                                        hero.setState(false);
                                        enemyBullet.setState(false);
                                        booms.add(new Boom(hx, hy));
                                    }
                                    break;
                                case 2:
                                    if (ebx >= hx && ebx <= hx + 50 && eby + 5 >= hy && eby <= hy + 47) {
                                        hero.setState(false);
                                        enemyBullet.setState(false);
                                        booms.add(new Boom(hx, hy));
                                    }
                                    break;
                                case 3:
                                    if (eby >= hy && eby <= hy + 50 && ebx <= hx + 52 && ebx + 5 >= hx) {
                                        hero.setState(false);
                                        enemyBullet.setState(false);
                                        booms.add(new Boom(hx, hy));
                                    }
                                    break;
                            }
                        }

                    }
                }
            }
        }
    }
}
